/**
	RSSChannel class - represents the information for a single RSS feed source as an RSSChannel object
	supports RSS 0.91, 1.0, and 2.0


	@author Chandler McWilliams
	@version 2006-05-18
	@usage
		<code>		
			// 1. load in an rss feed
			var rss = new RSSChannel();

			// 2. define a function to be called once the data is loaded - onLoad listens for this
			rss.onLoad = function (ok) {
				if (ok) {
					// this function for the example only
					processRSSData(this);
				}
			}
			// tell the object to load the data from a file or url
			rss.load("http://www.engadget.com/rss.xml");
			
			//the processing function takes in the rss object
			function processRSSData(rss) {
				trace("Link : "+rss.link);
				trace("Title : "+rss.title);
				trace("Description : "+rss.description);
				trace("Number of Entries : "+rss.items.length);
				trace("First Entry's Title : "+rss.items[0].title);
				trace("First Entry's Body : "+rss.items[0].description);
				
			}

		</code>

*/
import timber.rss.RSSHeadline;

class timber.rss.RSSChannel {
	
	var title;
	var link;
	var description;
	var language;
	var items
	var url;
	var xml;


	/**
		Constructor.
	**/
	public function RSSChannel() {
		this.title			= 'Untitled';
		this.link			= false;
		this.description	= '';
		this.language		= '';
		this.items			= new Array();
		this.url			= false;
		this.xml			= false;	
	}

	/**
		Load in an RSS feed.

		@param  (String) url. RSS feed web url.

	**/
	public function load(url:String):Void {
		this.url = url;
		// load and parse the XML
		this.xml = new XML();
		this.xml.parent = this;
		this.xml.ignoreWhite = true;
		//this.xml.output = output;			// make a reference to the parent object
		this.xml.onLoad = function (ok) {
			if (ok) {
				// parse the RSS into a data structure
				var kids, i, j, title, description, language, link, item, channelkids;
				
				// find channel node and get the data
				kids = this.firstChild.childNodes;
				
				for (i=0; i<kids.length; i++) {
					switch (kids[i].nodeName) {
						case 'channel':
							channelkids = kids[i].childNodes;
							for (j=0; j<channelkids.length; j++) {
								switch (channelkids[j].nodeName.toLowerCase()) {
									case 'title':
										this.parent.setTitle(channelkids[j].firstChild.nodeValue);
										break;
				
									case 'description':
										this.parent.setDescription(channelkids[j].firstChild.nodeValue);
										break;
			
									case 'language':
										this.parent.setLanguage(channelkids[j].firstChild.nodeValue);
										break;
			
									case 'link':
										this.parent.setLink(channelkids[j].firstChild.nodeValue);
										break;
			
										// if this is a RSS 2.0 or 0.91 feed the items are in the channel node
									case 'item':
										item = new RSSHeadline();
										item.initWithXML(channelkids[j].childNodes);
										this.parent.addItem(item);
										break;			
									}
								}
							break;

							// if this is a RSS 1.0 feed, we need to look for the item's in the root node
							case 'item':
								item = new RSSHeadline();
								item.initWithXML(kids[i].childNodes);
								this.parent.addItem(item);
								break;
						}
					} //end for
			
				this.parent.onLoad(true);
			} 
		}
		this.xml.load(this.url);	
	}


	// ===========================================================
	// - ACCESSOR FUNCTIONS
	// ===========================================================
	public function getTitle() {return this.title;}
	public function setTitle(title) {this.title = title;}

	public function getLink() {return this.link;}
	public function setLink(link) {this.link = link}

	public function getDescription() {return this.description;}
	public function setDescription(description) {this.description = description;}

	public function getLanguage() {return this.language;}
	public function setLanguage(language) {	this.language = language;}

	public function getItems() {return this.items;}
	public function addItem(RSSHeadline) {	this.items.push(RSSHeadline);}

	public function getURL() {return this.url;}
	public function getXML() {return this.xml;}

}